﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.OleDb;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Assembly
{
    public partial class panel_F4CheckRV201F06 : UserControl
    {
        private INISettings mySetting;
        private SqlConnection myConnection;
        public panel_F4CheckRV201F06()
        {
            InitializeComponent();
            mySetting = new INISettings();
            myConnection = new SqlConnection(mySetting.SQLConnectionString);
            LoadData();
            this.RV201F06.DoubleClick += RR732_DoubleClick;
            this.RV201F06.PopupMenuShowing += RR732_PopupMenuShowing;
            this.f4RV201F06ContextMenu.ItemClicked += f4RR732ContextMenu_ItemClicked;
        }

        /////////////////////////////
        /// Call Edit Form
        /////////////////////////////
        private void callEditForm()
        {
            DataRow row = RV201F06.GetFocusedDataRow();
            frm_Edit_F4CheckRV201F06 editForm = new frm_Edit_F4CheckRV201F06();
            editForm.serial = row[2].ToString();
            if (editForm.LoadData())
            {
                editForm.ShowDialog();
                //LoadData();
            }
            else
            {
                MessageBox.Show("Cannot edit selected row.");
            }
        }

        void f4RR732ContextMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            this.f4RV201F06ContextMenu.Hide();            
            switch (e.ClickedItem.Name)
            {
                case "editSelection":
                    callEditForm();
                    break;
                case "addSelection":
                    frm_Add_F4CheckRV201F06 add_Form = new frm_Add_F4CheckRV201F06();
                    add_Form.ShowDialog();
                    //LoadData();
                    break;                
                case "refreshSelection":
                    LoadData();
                    break;
            }
        }

        void RR732_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
        {
            this.f4RV201F06ContextMenu.Show(MousePosition);
        }

        private void RR732_DoubleClick(object sender, EventArgs e)
        {
            callEditForm();                        
        }
        
        //////////////////////////////////////////////////
        /// Load data from database to grid
        //////////////////////////////////////////////////
        private void LoadData()
        {
            DataSet sourceDataSet = new DataSet();
            SQLManagement mySQLManagement = new SQLManagement();
            mySQLManagement.SQLConnectionString = mySetting.SQLConnectionString;
            string sql = "SELECT * FROM [231_RV201F06]";
            sourceDataSet = mySQLManagement.SQL_Query(sql);
            if (sourceDataSet != null)
            {
                DataTable ShowTable = sourceDataSet.Tables[0].Clone();
                for (int i = 0; i < ShowTable.Columns.Count; i++)
                {
                    ShowTable.Columns[i].DataType = typeof(string);
                }
                foreach (DataRow check_row in sourceDataSet.Tables[0].Rows)
                {
                    ShowTable.ImportRow(check_row);
                }

                foreach (DataRow show_row in ShowTable.Rows)
                {
                    show_row.BeginEdit();
                    for (int i = 0; i < sourceDataSet.Tables[0].Columns.Count; i++)
                    {
                        if (string.IsNullOrEmpty(show_row[i].ToString().Trim()))
                        {
                            show_row[i] = "***";
                        }
                    }
                    show_row.EndEdit();
                }
                gridRV201F06.DataSource = ShowTable;                
            }
            else
            {
                DataTable newTable = new DataTable();
                newTable.Columns.Add("Serial No(RJ)", typeof(string));
                newTable.Columns.Add("Comp No#", typeof(string));
                newTable.Columns.Add("Serial No(RRC)", typeof(string));
                newTable.Columns.Add("Written(Name)", typeof(string));
                newTable.Columns.Add("Written(No#)", typeof(string));
                newTable.Columns.Add("Checked(Name)", typeof(string));
                newTable.Columns.Add("Checked(No#)", typeof(string));
                newTable.Columns.Add("Written Year", typeof(string));
                newTable.Columns.Add("Written Month", typeof(string));
                newTable.Columns.Add("Written Day", typeof(string));
                newTable.Columns.Add("Export Year", typeof(string));
                newTable.Columns.Add("Export Month", typeof(string));
                newTable.Columns.Add("Export Day", typeof(string));
                newTable.Columns.Add("Customer", typeof(string));
                newTable.Columns.Add("Remark", typeof(string));
                newTable.Columns.Add("Appearance Diagram", typeof(string));
                newTable.Columns.Add("Wiring Diagram", typeof(string));
                newTable.Columns.Add("RC-510 Serial", typeof(string));
                newTable.Columns.Add("SB020 Serial", typeof(string));
                newTable.Columns.Add("SB017A Serial", typeof(string));
                newTable.Columns.Add("Indicator1 Serial", typeof(string));
                newTable.Columns.Add("SB019(Door) Serial", typeof(string));
                newTable.Columns.Add("<Reserved>", typeof(string));
                newTable.Columns.Add("<Reserved>1", typeof(string));
                newTable.Columns.Add("<Reserved>2", typeof(string));
                newTable.Columns.Add("SB019(Stg1) Serial", typeof(string));
                newTable.Columns.Add("SB019(Stg2) Serial", typeof(string));
                newTable.Columns.Add("<Reserved>3", typeof(string));
                newTable.Columns.Add("<Reserved>4", typeof(string));
                newTable.Columns.Add("Indiactor2 Serial", typeof(string));
                newTable.Columns.Add("<Reserved>5", typeof(string));
                newTable.Columns.Add("<Reserved>6", typeof(string));
                newTable.Columns.Add("<Reserved>7", typeof(string));
                newTable.Columns.Add("Y-Axis Driver Serial", typeof(string));
                newTable.Columns.Add("Z-Axis Driver Serial", typeof(string));
                newTable.Columns.Add("CID Serial", typeof(string));
                newTable.Columns.Add("RC-510 Program File", typeof(string));
                newTable.Columns.Add("Program Version", typeof(string));
                newTable.Columns.Add("FPGA Version", typeof(string));
                newTable.Columns.Add("Initialize Data File", typeof(string));
                newTable.Columns.Add("Body No", typeof(string));
                newTable.Columns.Add("IP Adress", typeof(string));
                newTable.Columns.Add("MAC Address 1", typeof(string));
                newTable.Columns.Add("MAC Address 2", typeof(string));
                newTable.Columns.Add("MAC Address 3", typeof(string));
                newTable.Columns.Add("Y-Axis Position 1", typeof(string));
                newTable.Columns.Add("Y-Axis Position 2", typeof(string));
                newTable.Columns.Add("Y-Axis Position 3", typeof(string));
                newTable.Columns.Add("Y-Axis Position 4", typeof(string));
                newTable.Columns.Add("Y-Axis Position 5", typeof(string));
                newTable.Columns.Add("Y-Axis Position 6", typeof(string));
                newTable.Columns.Add("Z-Axis Position 1", typeof(string));
                newTable.Columns.Add("Z-Axis Position 2", typeof(string));
                newTable.Columns.Add("Z-Axis Position 3", typeof(string));
                newTable.Columns.Add("Z-Axis Position 4", typeof(string));
                newTable.Columns.Add("Z-Axis Position 5", typeof(string));
                newTable.Columns.Add("Z-Axis Position 6", typeof(string));
                newTable.Columns.Add("Mapping Top", typeof(string));
                newTable.Columns.Add("Mapping Bottom", typeof(string));
                newTable.Columns.Add("BCR Reading", typeof(string));
                newTable.Columns.Add("Stacker Hand Threshold", typeof(string));
                newTable.Columns.Add("Laser Prt# Threshold", typeof(string));
                newTable.Columns.Add("YAX1 Driver Ver", typeof(string));
                newTable.Columns.Add("ZAX1 Driver Ver", typeof(string));
                newTable.Columns.Add("RIO/Door Ver 1", typeof(string));
                newTable.Columns.Add("RIO/Door Ver 2", typeof(string));
                newTable.Columns.Add("RIO/Door Ver 3", typeof(string));
                newTable.Columns.Add("RIO/Door Ver 4", typeof(string));
                newTable.Columns.Add("RIO/Stage Ver 1", typeof(string));
                newTable.Columns.Add("RIO/Stage Ver 2", typeof(string));
                newTable.Columns.Add("RIO/Stage Ver 3", typeof(string));
                newTable.Columns.Add("RIO/Stage Ver 4", typeof(string));
                newTable.Columns.Add("RIO/Indicator1 Ver 1", typeof(string));
                newTable.Columns.Add("RIO/Indicator1 Ver 2", typeof(string));
                newTable.Columns.Add("RIO/Indicator1 Ver 3", typeof(string));
                newTable.Columns.Add("RIO/Indicator1 Ver 4", typeof(string));
                newTable.Columns.Add("RIO/Indicator2 Ver 1", typeof(string));
                newTable.Columns.Add("RIO/Indicator2 Ver 2", typeof(string));
                newTable.Columns.Add("RIO/Indicator2 Ver 3", typeof(string));
                newTable.Columns.Add("RIO/Indicator2 Ver 4", typeof(string));
                newTable.Columns.Add("CPLD0 Ver", typeof(string));
                newTable.Columns.Add("CPLD1 Ver", typeof(string));
                newTable.Columns.Add("Loader LD-510 Ver", typeof(string));

                gridRV201F06.DataSource = newTable;
            }
            RV201F06.Columns[0].Caption = "Serial No(RJ)";
            RV201F06.Columns[1].Caption = "Comp No.";
            RV201F06.Columns[2].Caption = "Serial No(RRC)";
            RV201F06.Columns[3].Caption = "Written(Name)";
            RV201F06.Columns[4].Caption = "Written(No.)";
            RV201F06.Columns[5].Caption = "Checked(Name)";
            RV201F06.Columns[6].Caption = "Checked(No.)";
            RV201F06.Columns[7].Caption = "Written Year";
            RV201F06.Columns[8].Caption = "Written Month";
            RV201F06.Columns[9].Caption = "Written Day";
            RV201F06.Columns[10].Caption = "Export Year";
            RV201F06.Columns[11].Caption = "Export Month";
            RV201F06.Columns[12].Caption = "Export Day";
            RV201F06.Columns[13].Caption = "Customer";
            RV201F06.Columns[14].Caption = "Remark";
            RV201F06.Columns[15].Caption = "Appearance Diagram";
            RV201F06.Columns[16].Caption = "Wiring Diagram";
            RV201F06.Columns[17].Caption = "RC-510 Serial";
            RV201F06.Columns[18].Caption = "SB020 Serial";
            RV201F06.Columns[19].Caption = "SB017A Serial";
            RV201F06.Columns[20].Caption = "Indicator1 Serial";
            RV201F06.Columns[21].Caption = "SB019(Door) Serial";
            RV201F06.Columns[22].Caption = "<Reserved>";
            RV201F06.Columns[23].Caption = "<Reserved>";
            RV201F06.Columns[24].Caption = "<Reserved>";
            RV201F06.Columns[25].Caption = "SB019(Stg1) Serial";
            RV201F06.Columns[26].Caption = "SB019(Stg2) Serial";
            RV201F06.Columns[27].Caption = "<Reserved>";
            RV201F06.Columns[28].Caption = "<Reserved>";
            RV201F06.Columns[29].Caption = "Indiactor2 Serial";
            RV201F06.Columns[30].Caption = "<Reserved>";
            RV201F06.Columns[31].Caption = "<Reserved>";
            RV201F06.Columns[32].Caption = "<Reserved>";
            RV201F06.Columns[33].Caption = "Y-Axis Driver Serial";
            RV201F06.Columns[34].Caption = "Z-Axis Driver Serial";
            RV201F06.Columns[35].Caption = "CID Serial";
            RV201F06.Columns[36].Caption = "RC-510 Program File";
            RV201F06.Columns[37].Caption = "Program Version";
            RV201F06.Columns[38].Caption = "FPGA Version";
            RV201F06.Columns[39].Caption = "Initialize Data File";
            RV201F06.Columns[40].Caption = "Body No";
            RV201F06.Columns[41].Caption = "IP Adress";
            RV201F06.Columns[42].Caption = "MAC Address 1";
            RV201F06.Columns[43].Caption = "MAC Address 2";
            RV201F06.Columns[44].Caption = "MAC Address 3";
            RV201F06.Columns[45].Caption = "Y-Axis Position 1";
            RV201F06.Columns[46].Caption = "Y-Axis Position 2";
            RV201F06.Columns[47].Caption = "Y-Axis Position 3";
            RV201F06.Columns[48].Caption = "Y-Axis Position 4";
            RV201F06.Columns[49].Caption = "Y-Axis Position 5";
            RV201F06.Columns[50].Caption = "Y-Axis Position 6";
            RV201F06.Columns[51].Caption = "Z-Axis Position 1";
            RV201F06.Columns[52].Caption = "Z-Axis Position 2";
            RV201F06.Columns[53].Caption = "Z-Axis Position 3";
            RV201F06.Columns[54].Caption = "Z-Axis Position 4";
            RV201F06.Columns[55].Caption = "Z-Axis Position 5";
            RV201F06.Columns[56].Caption = "Z-Axis Position 6";
            RV201F06.Columns[57].Caption = "Mapping Top";
            RV201F06.Columns[58].Caption = "Mapping Bottom";
            RV201F06.Columns[59].Caption = "BCR Reading";
            RV201F06.Columns[60].Caption = "Stacker Hand Threshold";
            RV201F06.Columns[61].Caption = "Laser Prt. Threshold";
            RV201F06.Columns[62].Caption = "YAX1 Driver Ver";
            RV201F06.Columns[63].Caption = "ZAX1 Driver Ver";
            RV201F06.Columns[64].Caption = "RIO/Door Ver 1";
            RV201F06.Columns[65].Caption = "RIO/Door Ver 2";
            RV201F06.Columns[66].Caption = "RIO/Door Ver 3";
            RV201F06.Columns[67].Caption = "RIO/Door Ver 4";
            RV201F06.Columns[68].Caption = "RIO/Stage Ver 1";
            RV201F06.Columns[69].Caption = "RIO/Stage Ver 2";
            RV201F06.Columns[70].Caption = "RIO/Stage Ver 3";
            RV201F06.Columns[71].Caption = "RIO/Stage Ver 4";
            RV201F06.Columns[72].Caption = "RIO/Indicator1 Ver 1";
            RV201F06.Columns[73].Caption = "RIO/Indicator1 Ver 2";
            RV201F06.Columns[74].Caption = "RIO/Indicator1 Ver 3";
            RV201F06.Columns[75].Caption = "RIO/Indicator1 Ver 4";
            RV201F06.Columns[76].Caption = "RIO/Indicator2 Ver 1";
            RV201F06.Columns[77].Caption = "RIO/Indicator2 Ver 2";
            RV201F06.Columns[78].Caption = "RIO/Indicator2 Ver 3";
            RV201F06.Columns[79].Caption = "RIO/Indicator2 Ver 4";
            RV201F06.Columns[80].Caption = "CPLD0 Ver";
            RV201F06.Columns[81].Caption = "CPLD1 Ver";
            RV201F06.Columns[82].Caption = "Loader LD-510 Ver";

            RV201F06.BestFitColumns();
        }

        private void bottonAdd_Click(object sender, EventArgs e)
        {
            frm_Add_F4CheckRV201F06 add_Form = new frm_Add_F4CheckRV201F06();
            add_Form.ShowDialog();
            //LoadData();
        }

        private void bottonEdit_Click(object sender, EventArgs e)
        {
            callEditForm();
        }

        private void buttonRefresh_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void buttonExport_Click(object sender, EventArgs e)
        {
            Cursor currentCursor = Cursor.Current;
            Cursor.Current = Cursors.WaitCursor;
            try
            {
                SaveFileDialog saveFDlg = new SaveFileDialog();
                saveFDlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                saveFDlg.Filter = "Microsoft Office Excel|*.xlsx";
                saveFDlg.Title = "Save As";
                saveFDlg.FileName = "RV201F06.xlsx";
                if (saveFDlg.ShowDialog(this) == DialogResult.OK)
                {
                    if (!string.IsNullOrEmpty(saveFDlg.FileName))
                    {
                        RV201F06.OptionsPrint.PrintDetails = true;
                        RV201F06.OptionsPrint.ExpandAllDetails = true;
                        RV201F06.OptionsPrint.AutoWidth = false;                        
                        Font default_font = new Font("Times New Roman", 10);
                        RV201F06.AppearancePrint.Row.Font = default_font;
                        RV201F06.AppearancePrint.HeaderPanel.Font = default_font;
                        RV201F06.OptionsPrint.UsePrintStyles = true;
                        //RV201F06.ExportToXlsx(saveFDlg.FileName);
                        DevExpress.XtraPrinting.XlsxExportOptions myOptions = new DevExpress.XtraPrinting.XlsxExportOptions();
                        myOptions.SheetName = "Data";
                        myOptions.TextExportMode = DevExpress.XtraPrinting.TextExportMode.Text;
                        myOptions.ShowGridLines = true;
                        RV201F06.ExportToXlsx(saveFDlg.FileName, myOptions);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Warning!");
            }
            finally
            {
                Cursor.Current = currentCursor;
            }
        }
    }
}
